package cn.gyouzhe.mapper.provider;

import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.jdbc.SQL;

/**
 * 商品provider
 *
 * @author wangchuan
 * @datetime 2018/3/4 23:20
 */
public class GoodProvider {
    /**
     * 返回商品查询SQL
     * @return sql
     */
    public String selectGoodInfo(@Param("pagekey") String pagekey, @Param("cid") int cid, @Param("type") int type, @Param("sort") String sort) {
        String sql = new SQL() {{
            SELECT("g.id", "g.good_id", "g.title",
                    "g.short_title", "g.introduce", "g.pic",
                    "g.price", "g.commission", "g.cid",
                    "g.tmall", "g.sell_num", "g.coupon_id",
                    "g.seller_id", "g.del", "c.price as couponprice",
                    "c.demand", "c.start_time", "c.end_time", "s.seller_name");
            FROM(" g_coupon c", "g_good g ");
            LEFT_OUTER_JOIN("g_seller s on g.seller_id=s.seller_id");
            WHERE("g.coupon_id=c.coupon_id");
            if(type != 0)
                WHERE("g.tmall=#{type}");
            switch(pagekey) {
                case "rqb":
                    WHERE("g.sell_num>10000");
                    break;
                case "jkj":
                    WHERE("(g.price-c.price<10)");
                    break;
                default:
                    break;
            }
            WHERE("c.price < g.price");
            if(cid != 0)
                WHERE("g.cid=#{cid}");

            if(StringUtils.equals(sort, "n")) {
                ORDER_BY("g.create_at DESC");
            } else if(StringUtils.equals(sort, "p")) {
                ORDER_BY("(g.price-c.price) ASC");
            } else {
                ORDER_BY("g.sell_num DESC");
            }
        }}.toString();
        return sql;
    }
}
